﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace PhanMemDao
{
    public class LyThuyetDAO
    {
        DataAccess da = new DataAccess();
        public string strWhere = "";
        public string strCondition = "";
        //Lay tat ca ten chuong cua mon hoc C
        public DataTable ten_chuong()
        {
            DataTable dt = new DataTable();
            string str = "Select * from khai_niem_cb";
            dt = da.lay_du_lieu(str);
            return dt;
        }
        public int ma_bai_viet(string ten_bai)
        {
            DataTable dt = new DataTable();
            string str = " Select ma_bhct from bai_hoc_ct where ten_bhct= N'" + ten_bai + "'";
            dt = da.lay_du_lieu(str);
            if (dt.Rows.Count > 0)
                return Convert.ToInt32(dt.Rows[0]["ma_bhct"]);
            else
                return -1;
        }
        //  Lay tat ca noi dung trong mot bai hoc
        public DataTable NoiDungBaiHoc(string tenbai)
        {
            int maBai = ma_bai_viet(tenbai);
            DataTable dt = new DataTable();
            if (maBai > 0)
            {
                string str = "Select * from bai_hoc_ct where ma_bhct = '" + maBai + "'";
                dt = da.lay_du_lieu(str);

            }
            else
            {
                string str = "select * from khai_niem_cb where ten_kn =N'" + tenbai + "'";
                dt = da.lay_du_lieu(str);
            }
            return dt;
        }
        public DataTable ten_bai_ghinho(string tenchuong)
        {
            DataTable dt = new DataTable();
            string str = "Select ten_bhct from bai_hoc_ct  where ten_kn = N'" + tenchuong + "'";
            dt = da.lay_du_lieu(str);
            return dt;
        }
        public void GhiNho(string chuong, string tenbai, string ghinho)
        {

            string sql = "update bai_hoc_ct set ghi_nho = N'" + ghinho + "' where  ten_bhct = N'" + tenbai + "' and ten_kn = N'" + chuong + "'";
            da.thao_tac_du_lieu(sql);
        }
        //Hien ghi nho
        public DataTable HienGhiNho(string tenchuong, string tenbai)
        {
            DataTable dt = new DataTable();
            string str = "Select ghi_nho from bai_hoc_ct where ten_kn = N'" + tenchuong + "' and ten_bhct = N'" + tenbai + "'";
            dt = da.lay_du_lieu(str);
            return dt;
        }
        // Tim kiem
        public DataTable TimKiemTV(string tenTV)
        {
            DataTable dt = new DataTable();
            string sql = "select ten_tv as 'Tên thư viện', ham as 'Tên hàm' from thu_vien where ten_tv like N'%" + tenTV + "%'";
            dt = da.lay_du_lieu(sql);
            return dt;
        }
        public DataTable TimKiemBV(string Loai, string tenBV)
        {
            DataTable dt = new DataTable();
             string sql = "select * from bai_hoc_ct";
             if (Loai.Length == 0)
                strWhere = " ten_bhct like N'%" + tenBV + "%'";
             if (Loai == "Cú pháp")
                strWhere =" cu_phap like N'%" + tenBV + "%'";
             if (Loai == "Chức năng")
                strWhere = " chuc_nang like N'%" + tenBV + "%'";
             if (Loai == "Quy tắc")
                strWhere = " quy_tac like N'%" + tenBV + "%'";
             if (strWhere.Length > 1)
                sql = sql +" Where " + strWhere;
            
             
            dt = da.lay_du_lieu(sql);
            return dt;
        }
        public void themvidu(string chuong, string tenbai, string ghinho)
        {

            string sql = "update bai_hoc_ct set vi_du = N'" + ghinho + "' where  ten_bhct = N'" + tenbai + "' and ten_kn = N'" + chuong + "'";
            da.thao_tac_du_lieu(sql);
        }
    }
}
